package com.molyfun.parents.fragment.dialog;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.UserResponse;
import com.molyfun.parents.utils.GsonUtil;
import com.molyfun.parents.utils.PreferencesStorageUtil;
import com.molyfun.parents.utils.UIUtils;
import com.molyfun.parents.view.common.MfEditText;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EditProfileDialogFragment extends DialogFragment implements View.OnClickListener {

    private static final String FLAG_RESTART = "need_restart";

    @InjectView(R.id.nick_name)
    MfEditText mNickName;
    @InjectView(R.id.cancel)
    TextView mCancel;
    @InjectView(R.id.commit)
    TextView mCommit;

    private boolean mNeedRestart = true;

    private static EditProfileDialogFragment newInstance() {
        EditProfileDialogFragment mStudentMenuDialogFragment = new EditProfileDialogFragment();
//        Bundle bundle = new Bundle();
//        mStudentMenuDialogFragment.setArguments(bundle);
        return mStudentMenuDialogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setCancelable(false);
//        setStyle(DialogFragment.STYLE_NORMAL, R.style.BaseDialog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_edit_profile, container, false);
        ButterKnife.inject(this, view);
        mCancel.setOnClickListener(this);
        mCommit.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                getDialog().dismiss();
                break;
            case R.id.commit:
                if (TextUtils.isEmpty(mNickName.getText().toString().trim())) {
                    UIUtils.showToast(getContext(), "请输入昵称");
                }
                updateUserInfo(mNickName.getText().toString().trim());
                break;
            default:
                break;
        }

    }



    private void updateUserInfo(String nickname){
        Call<UserResponse> call = MFPApplication.getInstance().getRestClient().updateInfo(mNickName.getText().toString().trim(), null);
        call.enqueue(new Callback<UserResponse>() {
            @Override
            public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
                if (response.body() != null && response.body().getData() != null) {
                    if(response.body().getCode() == 200){
                        MFPApplication.getInstance().setUser(response.body().getData());
                        PreferencesStorageUtil.setUserBean(getContext(), GsonUtil.toJson(response.body().getData()));
                        UIUtils.showToast(getContext(), "更新成功");
                        if(mEditProfileCallback != null){
                            mEditProfileCallback.onUpdateSuccess();
                        }
                        getDialog().dismiss();
                    }else{
                        UIUtils.showToast(getContext(), response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<UserResponse> call, Throwable t) {
                UIUtils.showToast(getContext(), "更新失败");
            }
        });
    }

    public interface EditProfileCallback{
        public void onUpdateSuccess();
    }

    public void setEditProfileCallback(EditProfileCallback editProfileCallback){
        this.mEditProfileCallback = editProfileCallback;
    }

    private EditProfileCallback mEditProfileCallback;

    public static class EditProfileDialogBuilder {

        EditProfileDialogFragment mEditProfileDialogFragment;
        private String mTag;

        public EditProfileDialogBuilder() {
            mEditProfileDialogFragment = EditProfileDialogFragment.newInstance();
            mTag = "edit_profile";
        }

        public EditProfileDialogBuilder setTag(String tag) {
            this.mTag = tag;
            return this;
        }

        public EditProfileDialogBuilder setEditProfileCallback(EditProfileCallback editProfileCallback) {
            this.mEditProfileDialogFragment.mEditProfileCallback = editProfileCallback;
            return this;
        }

        public void build(FragmentTransaction fragmentTransaction) {
            fragmentTransaction.add(mEditProfileDialogFragment, mTag);
            fragmentTransaction.commitAllowingStateLoss();
        }

    }
}
